package com.example.farmer.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.farmer.Activity.ClassPostActivity;
import com.example.farmer.Adapter.ViewPagerClassAdapter;
import com.example.farmer.Presenter.ClassPresenter;
import com.example.farmer.R;
import com.example.farmer.View.ClassView;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class ClassFragment extends Fragment implements ClassView{
    private ClassPresenter mPresenter;
    private ViewPager2 viewPager;
    private TextView textView;
    private ImageButton imageButton;
    private TabLayout tabLayout;
    private ViewPagerClassAdapter viewPagerAdapter;

    public ClassFragment() {
        // Required empty public constructor
    }

    public static ClassFragment newInstance(String param1, String param2) {
        ClassFragment fragment = new ClassFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.activity_class, container, false);


        viewPager = view.findViewById(R.id.viewPager);
        tabLayout = view.findViewById(R.id.tabs);
        textView = view.findViewById(R.id.toolbar_title);
        imageButton = view.findViewById(R.id.issue);
        viewPagerAdapter = new ViewPagerClassAdapter(this);
        viewPager.setAdapter(viewPagerAdapter);
        mPresenter = new ClassPresenter(this);

        // 初始化数据
        initData();
        textView.setText("学堂");

        // 设置TabLayout与ViewPager2关联
        tabLayout.addTab(tabLayout.newTab().setText("热点新闻"));
//        tabLayout.addTab(tabLayout.newTab().setText("专家咨询"));
        tabLayout.addTab(tabLayout.newTab().setText("海报圈新闻"));
        tabLayout.addTab(tabLayout.newTab().setText("创业项目"));
        tabLayout.addTab(tabLayout.newTab().setText("动植物疫情预报"));
        tabLayout.addTab(tabLayout.newTab().setText("科技百科"));
        tabLayout.addTab(tabLayout.newTab().setText("气象预报"));
        // 添加其他Tab


        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // 当Tab被选中时，获取其位置并通知Presenter
                int position = tab.getPosition();
                viewPager.setCurrentItem(position);
                mPresenter.onTabSelected(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // 可以不实现
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 可以不实现
            }
        });

        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                tabLayout.getTabAt(position).select();
            }
        });
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ClassPostActivity.class);
                startActivity(intent);
            }
        });
        return view;
    }

    private void initData() {
        // 模拟数据初始化，你需要根据实际情况来初始化数据
        viewPagerAdapter.setNewsList(getSampleList());
//        viewPagerAdapter.setConsultationsList(getSampleList());
        viewPagerAdapter.setPosterCircleNewsList(getSampleList());
        viewPagerAdapter.setStartupProjectsList(getSampleList());
        viewPagerAdapter.setAnimalPlantEpidemicList(getSampleList());
        viewPagerAdapter.setScienceEncyclopediaList(getSampleList());
        viewPagerAdapter.setWeatherForecastList(getSampleList());
    }

    private List<String> getSampleList() {
        List<String> list = new ArrayList<>();
        for (int i = 1; i <= 7; i++) {
            list.add("Item " + i);
        }
        return list;
    }


    // 实现ClassView接口方法
    @Override
    public void showHotNews(List<String> newsList) {
        // 更新热点新闻UI
        viewPagerAdapter.setNewsList(newsList);
        viewPager.setCurrentItem(0);
    }

//    @Override
//    public void showExpertConsultations(List<String> consultationsList) {
//        // 更新专家咨询UI
//        viewPagerAdapter.setConsultationsList(consultationsList);
//        viewPager.setCurrentItem(1);
//    }

    @Override
    public void showPosterCircleNews(List<String> posterCircleNewsList) {
        // 更新海报圈新闻UI
        viewPagerAdapter.setPosterCircleNewsList(posterCircleNewsList);
        viewPager.setCurrentItem(1);
    }

    @Override
    public void showStartupProjects(List<String> startupProjectsList) {
        // 更新创业项目UI
        viewPagerAdapter.setStartupProjectsList(startupProjectsList);
        viewPager.setCurrentItem(2);
    }

    @Override
    public void showAnimalPlantEpidemic(List<String> animalPlantEpidemicList) {
        // 更新动植物疫情预报UI
        viewPagerAdapter.setAnimalPlantEpidemicList(animalPlantEpidemicList);
        viewPager.setCurrentItem(3);
    }

    @Override
    public void showScienceEncyclopedia(List<String> scienceEncyclopediaList) {
        // 更新科技百科UI
        viewPagerAdapter.setScienceEncyclopediaList(scienceEncyclopediaList);
        viewPager.setCurrentItem(4);
    }

    @Override
    public void showWeatherForecast(List<String> weatherForecastList) {
        // 更新气象预报UI
        viewPagerAdapter.setWeatherForecastList(weatherForecastList);
        viewPager.setCurrentItem(5);
    }

    @Override
    public void setPresenter(ClassPresenter classPresenter) {
        this.mPresenter = classPresenter;
    }

}